﻿#MIME - Mega Man III Multi Editor v0.1a
#
# Copyright (c) 2007 Devon Meunier, Myopic Void
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the organization nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY DEVON MEUNIER ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL DEVON MEUNIER BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import wx
import xml.dom.minidom as xml

class SoundTrack:
	'''An object created from parsed XML data.'''
	def __init__(self):
		self.table = {} #hi byte / lo byte table
	
		resource_file = open("soundtrack.xml")
		data_set = xml.parse(resource_file)
		
		bank = int(data_set.getElementsByTagName("bank")[0].firstChild.data[2:],16)		
		names = data_set.getElementsByTagName("name")
		hi_bytes = data_set.getElementsByTagName("hi")
		lo_bytes = data_set.getElementsByTagName("lo")
		
		for index in range(0,len(names)):
			name = str(names[index].firstChild.data)
			hi = int(hi_bytes[index].firstChild.data, 16)
			lo = int(lo_bytes[index].firstChild.data, 16)
			
			self.table[(hi,lo)] = name
			
		self.bank = bank #address attribute
		
class MegaMan:
	'''An instance of a Mega Man rom and the functions to modify it.'''
	def __init__(self):
		self.soundtrack = SoundTrack()
		self.open = False
	
	def read_soundtable(self):
		'''Return a dictionary of track numbers and their hi-lo byte pairs.'''
		soundtable = {}
		self.rom.seek(self.soundtrack.bank)
		for track in range(1,8):
			soundtable[track] = (ord(self.rom.read(1)),ord(self.rom.read(1)))
		return soundtable

class MainWindow(wx.Frame):
	'''A window object'''
	def __init__(self,parent,id,title):	
		wx.Frame.__init__(self,parent,wx.ID_ANY, title, size = (640,480),
			style=wx.DEFAULT_FRAME_STYLE^(wx.RESIZE_BORDER|wx.MAXIMIZE_BOX))
		
		#Mega Man
		self.MegaMan = MegaMan()
		
		#ICON
		self.SetIcon(wx.Icon('C:\Users\Devon\Documents\MIME\icon.png',
			wx.BITMAP_TYPE_PNG))
		self.CreateStatusBar() # A Statusbar in the bottom of the window

		# Creating the "File" menu.
		file_menu= wx.Menu()
		
		open = file_menu.Append(wx.ID_ANY, "&Open"," Open Mega Man ROM")
		file_menu.AppendSeparator()
		quit = file_menu.Append(wx.ID_ANY,"E&xit"," Quit MIME")
		
		# Creating the "Help" menu
		help_menu = wx.Menu()
		
		about = help_menu.Append(wx.ID_ABOUT, "&About"," About MIME")
		
		# Creating the menubar.
		menu_bar = wx.MenuBar()
		menu_bar.Append(file_menu,"&File")
		menu_bar.Append(help_menu,"&Help")
		self.SetMenuBar(menu_bar)
		
		#Tab interface
		nb = wx.Notebook(self, -1)
		
		self.sound_panel = wx.Panel(nb, -1)
		sprite_panel = wx.Panel(nb, -1)
		
		nb.AddPage(self.sound_panel, "Audio")
		nb.AddPage(sprite_panel, "Sprites")
		
		#Sound Panel Contents

		#Event Bindings
		self.Bind(wx.EVT_MENU, self.on_exit, quit)
		self.Bind(wx.EVT_MENU, self.on_open, open)
		self.Bind(wx.EVT_MENU, self.on_about, about)

		self.Show(True)
		
	def on_about(self,e):
		'''About pop-up'''
		pop_up = wx.MessageDialog(self,
			"MIME v0.1a\nCopyright 2007 Devon Meunier",	"About MIME", wx.OK)
		pop_up.ShowModal()
		pop_up.Destroy()
		
	def on_exit(self,e):
		'''Quit MIME'''
		self.Close(True)
		
	def on_open(self, e):
		'''Open Mega Man III ROM'''
		directory = ''
		open_dialog = wx.FileDialog(self,'Open',directory,'','*.*',wx.OPEN)
		if open_dialog.ShowModal() == wx.ID_OK:
			filename = open_dialog.GetFilename()
			directory = open_dialog.GetDirectory()
			rom = open(directory + '\\' + filename, 'a+b')
			 
			self.MegaMan.rom = rom
			 
			#load audio data
			d = self.MegaMan.read_soundtable()
			table = self.MegaMan.soundtrack.table
			
			list = []
			for key in d:
				list.append(table[d[key]])
			self.boxlist = []	
			for i in range(len(list)):
				self.boxlist.append(wx.ComboBox(self.sound_panel, -1, value=list[i], choices=list, style= wx.CB_READONLY, pos=(0,i*20)))	
		
		open_dialog.Destroy()
		
app = wx.App()
frame = MainWindow(None, -1, "MIME - Mega Man III Multi Editor")
app.MainLoop()